package com.gtlab1207.br_awareness.common;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @description：AES加密解密转译
 * @modified By：
 * @version: 1.0
 */
public class AESUtil {

    private static final String ENCRYPT_CHARSET = "UTF-8";
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
    private static final String CIPHER_ALGORITHM_CS7= "AES/ECB/PKCS7Padding";//默认的加密算法
    private static final String PASSWORD = "yuwxzhjbxz23asd8";//密码器 需要与前端一致ßß





    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    /**
     * 加密，原始16位密码器
     * @param sSrc
     * @return
     */
    public static String Encrypt(String sSrc) {
        try {
            if(sSrc!=null&&!"".equals(sSrc)){
                byte[] raw = PASSWORD.getBytes(ENCRYPT_CHARSET);
                SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//"算法/模式/补码方式"
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
                byte[] encrypted = cipher.doFinal(sSrc.getBytes(ENCRYPT_CHARSET));

                return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
            }else{
                return null;
            }


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 解密，原始16位密码器
     * @param sSrc
     * @return
     */
    public static String Decrypt(String sSrc) {
        try {
            if(sSrc!=null&&!"".equals(sSrc)){
                byte[] raw = PASSWORD.getBytes(ENCRYPT_CHARSET);
                SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
                byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, ENCRYPT_CHARSET);
                return originalString;
            }else{
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
